#唐门优测
#道长最帅666
import yaml
import os
import pytest
from common.read_yaml import get_yaml_filepath
from common.readCofig import Envison
from common.route import *
from api import client
from api.client import HttpClient
import configparser
clent1 = HttpClient()

data = get_yaml_filepath(Envison=Envison)
data0 = login['case_data'][0]['body']

def login1(username = data0['username'],password = data0['password']):
    etc = {
        "username":username,
        "password":password
    }

    url = login['url']
    method = login['method']
    url = clent1.get_full_url(url,etc=etc)
    return clent1.send(url,method=method)

class Login(client.HttpClient):

    def __init__(self,admin_info,token):
        self.username = admin_info['user_name']
        self.password = admin_info['password']
        result = login1(username=self.username,password=self.password)
        # print("result3:",result)
        self.toekn = result['data']['token']
        #把token值写入配置文件中
        self.con_file = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + r"/configs/token.ini")
        config = configparser.ConfigParser()

        #打开配置文件
        config.read(self.con_file,encoding='utf-8')
        #在配置文件中设置token的值
        config.set("token","{}".format(token),str(self.toekn))
        config.write(open(self.con_file,'w')) #w+：修改配置文件的内容并保存



def login_zs(admin_info,zs_token):
    return Login(admin_info,zs_token)

def login_ls(admin_info,ls_token):
    return Login(admin_info,ls_token)